home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / ACORNUSERS / EMULATOR / NIB2DSK / c / nib2dsk
Text File  |  1998-02-05  |  6KB  |  189 lines

  1. /****************************************************************************************/
  2. /* Nib2Dsk - convert Apple II nibble order disk images to DOS order images              */
  3. /*                                                                                      */
  4. /* Original C++ source EMU2EM.CPP by Dan Scholnik   dpscholn@mtu.edu                    */
  5. /*                                                                                      */
  6. /* Translation to ANSI C, minor modifications, and port to RiscOS                       */
  7. /*                                by M.I.K.e        mlkoenig@linguistik.uni-erlangen.de */
  8. /* Last change: 1998-2-5                                                                */
  9. /****************************************************************************************/
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. #define    addr_mask    0x55
  15. #define    mask1          0x03
  16. #define    mask2        0x0C
  17. #define    mask3        0x30
  18. #define    mark        0xD5
  19.  
  20. int main(int argc, char *argv[])
  21. {
  22.     FILE *outfile;
  23.     FILE *infile;
  24.     unsigned char nibble_high[256], nibble_low[86], bytes[256];
  25.     unsigned char trans_table[] =
  26.                     { 0x96, 0x97, 0x9A, 0x9B, 0x9D, 0x9E, 0x9F, 0xA6,
  27.                          0xA7, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB2, 0xB3,
  28.                       0xB4, 0xB5, 0xB6, 0xB7, 0xB9, 0xBA, 0xBB, 0xBC,
  29.                       0xBD, 0xBE, 0xBF, 0xCB, 0xCD, 0xCE, 0xCF, 0xD3,
  30.                       0xD6, 0xD7, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE,
  31.                       0xDF, 0xE5, 0xE6, 0xE7, 0xE9, 0xEA, 0xEB, 0xEC,
  32.                       0xED, 0xEE, 0xEF, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6,
  33.                       0xF7, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF };
  34.     unsigned char reverse_bits[] = {0x00, 0x02, 0x01, 0x03};
  35.     unsigned char interleave[] =
  36.                     { 0x00, 0x07, 0x0E, 0x06, 0x0D, 0x05, 0x0C, 0x04,
  37.                       0x0B, 0x03, 0x0A, 0x02, 0x09, 0x01, 0x08, 0x0F };
  38.     int x, y, byte_count, nibble_count;
  39.     unsigned int header;
  40.     long file_count,pos;
  41.     unsigned char nibble_in,nibble_trans,byte_out,track_even, track_odd, track;
  42.     unsigned char chksum, sector_odd, sector_even, sector, last_nibble;
  43.     unsigned char volume, volume_odd, volume_even;
  44.     char command[30];
  45.  
  46.     if (argc!=3)
  47.     {
  48.         printf("Usage: nib2dsk infile outfile\n");
  49.         exit(1);
  50.     }
  51.     else
  52.     {
  53.         infile = fopen(argv[1], "rb");
  54.         if (infile == NULL)
  55.         {
  56.             printf("Input file does not exist.\n");
  57.             exit(2);
  58.         }
  59.         
  60.         fseek(infile, 0, SEEK_END);
  61.         if(ftell(infile) != 232960)
  62.         {
  63.           printf("Input file is no nibble order image.\n");
  64.           exit(5);
  65.         }
  66.         rewind(infile);
  67.         
  68.         outfile = fopen(argv[2], "wb");
  69.         if (outfile == NULL)
  70.         {
  71.             printf("Could not create output file.\n");
  72.             exit(3);
  73.         }
  74.     }
  75.     /* fill output file with 143360 bytes */
  76.     for(x=0;x<35;x++)
  77.         for(y=0;y<16;y++)
  78.             fwrite(bytes, 256, 1, outfile);
  79.     rewind(outfile);
  80.  
  81.   for(x=0;x<(35*16);x++)
  82.   {
  83.     byte_count=nibble_count=header=0;
  84.     /* seek to address field */
  85.     nibble_in=0;
  86.     while (!header && !feof(infile))
  87.     {
  88.         while ((nibble_in!=mark) && !feof(infile))
  89.             nibble_in = fgetc(infile);
  90.         nibble_in = fgetc(infile);
  91.         if (nibble_in==0xAA)
  92.         {
  93.             nibble_in = fgetc(infile);
  94.             if (nibble_in==0x96)
  95.                 header=1;
  96.         }
  97.     }
  98.     if (feof(infile)) break;
  99.     /* get volume */
  100.     volume_odd = fgetc(infile);
  101.     volume_even = fgetc(infile);
  102.     volume_odd&=addr_mask;
  103.     volume_odd<<=1;
  104.     volume_even&=addr_mask;
  105.     volume=volume_odd|volume_even;
  106.     /* get track# */
  107.     track_odd = fgetc(infile);
  108.     track_even = fgetc(infile);
  109.     track_odd&=addr_mask;
  110.     track_odd<<=1;
  111.     track_even&=addr_mask;
  112.     track=track_odd|track_even;
  113.     /* get sector# */
  114.     sector_odd = fgetc(infile);
  115.     sector_even = fgetc(infile);
  116.     sector_odd&=addr_mask;
  117.     sector_odd<<=1;
  118.     sector_even&=addr_mask;
  119.     sector=sector_odd|sector_even;
  120.     /* get checksum and check
  121.        get epilogue and check */
  122.  
  123.     /* seek to data field */
  124.     header=0;
  125.     while (!header && !feof(infile))
  126.     {
  127.         while ((nibble_in!=mark) && !feof(infile))
  128.             nibble_in = fgetc(infile);
  129.         nibble_in = fgetc(infile);
  130.         if (nibble_in==0xAA)
  131.         {
  132.             nibble_in = fgetc(infile);
  133.             if (nibble_in==0xAD)
  134.                 header=1;
  135.         }
  136.     }
  137.     if (feof(infile)) break;
  138.     /* read in 342 bytes data and do cumulative xor */
  139.     last_nibble=0;
  140.  
  141.     pos = ftell(infile);
  142.  
  143.     for(nibble_count=0;nibble_count<86;nibble_count++)
  144.     {
  145.         nibble_in = fgetc(infile);
  146.         nibble_trans=0;
  147.         while (trans_table[nibble_trans]!=nibble_in)
  148.             nibble_trans++;
  149.         last_nibble=nibble_trans^last_nibble;
  150.         nibble_low[nibble_count]=last_nibble;
  151.     }
  152.     for(nibble_count=0;nibble_count<256;nibble_count++)
  153.     {
  154.         nibble_in = fgetc(infile);
  155.         nibble_trans=0;
  156.         while (trans_table[nibble_trans]!=nibble_in)
  157.             nibble_trans++;
  158.         last_nibble=nibble_trans^last_nibble;
  159.         nibble_high[nibble_count]=last_nibble<<2;
  160.     }
  161.     /* get checksum byte and xor with last data. should be 0 */
  162.     nibble_in = fgetc(infile);
  163.     nibble_trans=0;
  164.     while (trans_table[nibble_trans]!=nibble_in)
  165.         nibble_trans++;
  166.     chksum=last_nibble^nibble_trans;
  167.     /* put bytes back together */
  168.     for(byte_count=0;byte_count<256;byte_count++)
  169.     {
  170.         if (byte_count<86)
  171.             bytes[byte_count]=nibble_high[byte_count]|reverse_bits[(nibble_low[byte_count]&mask1)];
  172.         else if (byte_count<172)
  173.             bytes[byte_count]=nibble_high[byte_count]|reverse_bits[((nibble_low[byte_count-86]&mask2)>>2)];
  174.         else
  175.             bytes[byte_count]=nibble_high[byte_count]|reverse_bits[((nibble_low[byte_count-172]&mask3)>>4)];
  176.     }
  177.     fseek(outfile, (((long)track*16)+(long)interleave[sector])*256, SEEK_SET);
  178.     fwrite(bytes, 256, 1, outfile);
  179.   };
  180.   fclose(outfile);
  181.   fclose(infile);
  182.   
  183.   printf("Volume: %d\n", volume);
  184.   sprintf(command, "Set Nib2Dsk$Volume %d", volume);
  185.   system(command);
  186.  
  187.   return(0);
  188. }
  189.